package com.example.nbshangpping.login;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.nbshangpping.R;
import com.example.nbshangpping.utils.BaseActivity;
import com.example.nbshangpping.utils.SaveUserUtils;
import com.example.nbshangpping.utils.URLUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

public class PwdQuestionActivity extends BaseActivity {
    private UserBean.DataBean userInfo;
    EditText answerEt;
    TextView questionTv;
    Button ensureBtn,backBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pwd_question2);
        userInfo= SaveUserUtils.getUserInfo(this);
        initView();
        setListener();
        setView();
    }

    private void setView() {
        questionTv.setText(userInfo.getQuestion());
    }

    private void setListener() {
        ensureBtn.setOnClickListener(listener);
        backBtn.setOnClickListener(listener);
    }
    View.OnClickListener listener= new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            int id=v.getId();
            switch (id) {
                case R.id.frag_pwd_btn:
                    String answer = answerEt.getText().toString();
                {                        Map<String,String> map = new HashMap<>();
                    map.put("id",userInfo.getId()+"");
                    map.put("answer",answer);
                    postNetwork(URLUtils.verifyAnswer_url,map);
                }
                    break;
                case R.id.frag_pwd_btnback:
                    finish();
                    break;
            }
        }
    };
    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
       UserBean jsonbean = new Gson().fromJson(result, UserBean.class);
        switch (jsonbean.getCode()) {
            case 200:
                SaveUserUtils.saveUserToFile(this,userInfo);
                userInfo = SaveUserUtils.getUserInfo(this);
                Toast.makeText(PwdQuestionActivity.this,"密保验证成功！", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(PwdQuestionActivity.this, ChangePwdActivity.class);
                startActivity(intent);
                break;
            case 201:
                Toast.makeText(PwdQuestionActivity.this,"参数错误！",Toast.LENGTH_SHORT).show();
            default:
                                Toast.makeText(PwdQuestionActivity.this,"密保错误！",Toast.LENGTH_SHORT).show();
               break;
                       }
    }


    private void initView() {
        answerEt = findViewById(R.id.frag_pwd_tel2);
        questionTv = findViewById(R.id.frag_pwd_tv4);
        ensureBtn = findViewById(R.id.frag_pwd_btn);
        backBtn=findViewById(R.id.frag_pwd_btnback);
    }
}